import 'package:flutter/material.dart';

import './tab_profile/index.dart';
import './tab_bookshelf/index.dart';
import './tab_bookstore/index.dart';
import '../../utils/event_bus.dart';
import '../../config.dart';
// 测试 TabBookShelf()
import '../noveldetail/index.dart';

List<Widget> tabview = [NovelDetailPage(novelId: '1',), TabBookStore(), TabProfile()];

List<BottomNavigationBarItem> barItemList = [
  BottomNavigationBarItem(label: '书架', icon: Icon(Icons.home)),
  BottomNavigationBarItem(label: '书城', icon: Icon(Icons.search)),
  BottomNavigationBarItem(label: '我的', icon: Icon(Icons.account_circle)),
];

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  int _selectedIndex = 0;
  double _bottom = -60;

  late final AnimationController _controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  )..repeat();

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    eventBus.on(Config.EventToggleTabBarIndex, (arg) {
      setState(() {
        _selectedIndex = arg;
      });
    });
    eventBus.on(Config.BottomNavigationBar, (arg) {
      setState(() {
        _bottom = arg;
      });
    });
  }

  @override
  void dispose() {
    eventBus.off(Config.EventToggleTabBarIndex);
    eventBus.off(Config.BottomNavigationBar);
    setState(() {
      this._bottom = 0;
    });
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return Stack(
            fit: StackFit.expand,
            children: [
              tabview[_selectedIndex],
              Container(
                height: 300,
              ),
              Positioned(
                left: 0,
                right: 0,
                bottom: _bottom,
                child: BottomNavigationBar(
                    items: barItemList,
                    currentIndex: _selectedIndex,
                    type: BottomNavigationBarType.fixed,
                    selectedItemColor: Colors.green,
                    onTap: _onItemTapped),
              ),
            ],
          );
        },
      ),
    );
    /* return Scaffold(
      body: tabview[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
          items: barItemList,
          currentIndex: _selectedIndex,
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Colors.green,
          onTap: _onItemTapped),
    ); */
  }
}
